package com.wtwd.device.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppHealthTypeContentMapper;
import com.wtwd.device.model.entity.ExcelImportLanguage;
import com.wtwd.device.model.entity.HealthTypeContent;
import com.wtwd.device.service.AppFileService;
import com.wtwd.device.service.AppHealthTypeContentService;
import com.wtwd.device.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2023-08-16
 */
@Service
public class AppHealthTypeContentServiceImpl extends ServiceImpl<AppHealthTypeContentMapper, HealthTypeContent> implements AppHealthTypeContentService {

    @Value("${healthType.excel}")
    private String healthTypePath;

    @Value("${file.path}")
    private String filePath;

    @Value("${healthContent.excel}")
    private String healthContentPath;

    @Resource
    private AppHealthTypeContentMapper healthTypeContentMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private AppFileService appFileService;

    @Override
    public List<HealthTypeContent> queryPageByTypeId(Integer pageNo, Integer pageSize, Integer typeId, String languageType)  throws Exception{
        Integer start = pageNo * pageSize;
        List<HealthTypeContent> healthTypeContents = healthTypeContentMapper.queryPageByTypeId(start, pageSize, typeId);
        return translatorsTypeLanguage(healthTypeContents,languageType);
    }

    @Override
    public List<HealthTypeContent> translatorsTypeLanguage(List<HealthTypeContent> healthTypeContents, String languageType) throws Exception {
        if (UNTRANSLATED_LANGUAGE.equals(languageType)) {//如果是中文不进行翻译
            return healthTypeContents;
        }
        if (healthTypeContents != null && healthTypeContents.size() != 0) {
            Map<String, Object> cache = redisService.getHashValue(HEATH_CONTENT_KEY_PREFIX);
            for (HealthTypeContent healthTypeContent : healthTypeContents) {
                String cacheLanguage = (String) cache.get(healthTypeContent.getId().toString());
                if (StringUtils.isNotBlank(cacheLanguage)) {//缓存文件的数据不为空,命中缓存开始翻译语言
                    List<ExcelImportLanguage> languagesData = JSONArray.parseArray((String) cache.get(healthTypeContent.getId().toString()), ExcelImportLanguage.class);
                    List<ExcelImportLanguage> collect = languagesData.stream().filter(e -> e.getType().equals(languageType)).collect(Collectors.toList());
                    ExcelImportLanguage language =null;
                    if (collect!=null&&collect.size()>0){
                        language=collect.get(0);
                    }else {//查询默认语言
                        List<ExcelImportLanguage> collect1 = languagesData.stream().filter(e -> e.getType().equals(DEFAULT_LANGUAGE_TYPE)).collect(Collectors.toList());
                        if (collect1 != null&&collect1.size()>0) {
                            language=collect1.get(0);
                        }
                    }
                    if (language!=null){
                        if (StringUtils.isNotBlank(language.getTranslation())){
                            healthTypeContent.setContent(language.getTranslation());
                        }
                        if (StringUtils.isNotBlank(language.getTitle())){//没有则运用原有title值
                            healthTypeContent.setTitle(language.getTitle());
                        }
                    }
                } else {//为空则命中文件,文件如果存在且命中,即添加到缓存
                    if (StringUtils.isNotBlank(healthTypeContent.getPath())) {
                        List<ExcelImportLanguage> list = appFileService.readExcel(filePath.concat(healthContentPath).concat(healthTypeContent.getPath()));
                        if (list != null && list.size() > 0) {
                            List<ExcelImportLanguage> collect = list.stream().filter(e -> e.getType().equals(languageType)).collect(Collectors.toList());
                            ExcelImportLanguage language =null;
                            if (collect!=null&&collect.size()>0){
                                language=collect.get(0);
                            }else {
                                List<ExcelImportLanguage> collect1 = list.stream().filter(e -> e.getType().equals(DEFAULT_LANGUAGE_TYPE)).collect(Collectors.toList());
                                if (collect1 != null&&collect1.size()>0) {
                                    language=collect1.get(0);
                                }
                            }
                            if (language != null) {
                                if (StringUtils.isNotBlank(language.getTranslation())){
                                    healthTypeContent.setContent(language.getTranslation());
                                }
                                if (StringUtils.isNotBlank(language.getTitle())){//没有则运用原有title值
                                    healthTypeContent.setTitle(language.getTitle());
                                }
                            }
                            redisService.saveCache(HEATH_CONTENT_KEY_PREFIX, healthTypeContent.getId().toString(), JSONObject.toJSONString(list));
                        }
                    }
                }
            }
        }
        return healthTypeContents;
    }


}
